﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using GMusicUtil.Common.Contracts;
using GMusicUtil.Common.ViewModels;
using NLog;

namespace GMusicUtil
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application, IShell
    {
        private static readonly Logger Log = LogManager.GetCurrentClassLogger();

        public App()
        {
            UIDispatcher = Dispatcher.CurrentDispatcher;

            BaseViewModel.Initialize(this);
        }

        private Dispatcher UIDispatcher { get; set; }

        #region IShell Members

        public void RunOnUiThread(Action action)
        {
            try
            {
                if (Dispatcher.CheckAccess())
                {
                    action();
                }
                else
                {
                    Dispatcher.Invoke(action);
                }
            }
            catch (Exception e)
            {
                Log.ErrorException("Dispatch Failed", e);
            }
        }

        public void RunOffUiThread(Action action)
        {
            ThreadPool.QueueUserWorkItem(_ => action());
        }

        #endregion
    }
}
